package com.example.myapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class TwoScortingActivity extends AppCompatActivity {

    // Team A
    private TextView teamAScoreView;
    private int scoreTeamA = 0;

    // Team B
    private TextView teamBScoreView;
    private int scoreTeamB = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two_scorting);

        // 初始化Team A
        teamAScoreView = findViewById(R.id.team_a_score);
        setupTeamButtons(R.id.team_a_3points, R.id.team_a_2points, R.id.team_a_free_throw, true);

        // 初始化Team B
        teamBScoreView = findViewById(R.id.team_b_score);
        setupTeamButtons(R.id.team_b_3points, R.id.team_b_2points, R.id.team_b_free_throw, false);

        // 重置按钮
        Button resetButton = findViewById(R.id.reset_button);
        resetButton.setOnClickListener(v -> {
            scoreTeamA = 0;
            scoreTeamB = 0;
            updateScores();
        });
    }

    private void setupTeamButtons(int btn3Id, int btn2Id, int freeThrowId, boolean isTeamA) {
        Button btn3Points = findViewById(btn3Id);
        Button btn2Points = findViewById(btn2Id);
        Button btnFreeThrow = findViewById(freeThrowId);

        View.OnClickListener listener = v -> {
            int viewId = v.getId();

            if (viewId == R.id.team_a_3points || viewId == R.id.team_b_3points) {
                addScore(isTeamA, 3);
            } else if (viewId == R.id.team_a_2points || viewId == R.id.team_b_2points) {
                addScore(isTeamA, 2);
            } else if (viewId == R.id.team_a_free_throw || viewId == R.id.team_b_free_throw) {
                addScore(isTeamA, 1);
            }
        };

        btn3Points.setOnClickListener(listener);
        btn2Points.setOnClickListener(listener);
        btnFreeThrow.setOnClickListener(listener);
    }

    private void addScore(boolean isTeamA, int points) {
        if (isTeamA) {
            scoreTeamA += points;
        } else {
            scoreTeamB += points;
        }
        updateScores();
    }

    private void updateScores() {
        teamAScoreView.setText(String.valueOf(scoreTeamA));
        teamBScoreView.setText(String.valueOf(scoreTeamB));
    }
}